/**
 * Copyright (C) 2010 D. Zoller, M. Richter, M. Blumenstein & W. Biller
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.siafu.rest;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.googlecode.siafu.rest.commands.GetContextCommand;
import com.googlecode.siafu.rest.commands.GetTrackableCommand;
import com.googlecode.siafu.rest.commands.UpdateContextCommand;
import com.googlecode.siafu.rest.commands.UpdateTrackableCommand;
import com.googlecode.siafu.rest.model.Context;
import com.googlecode.siafu.rest.model.Trackable;


/**
 * 
 * @author D. Zoller
 * @version $Id: TrackableResource.java,v 1.10 2010-06-15 14:58:25 nosebrain Exp $
 *
 * @param <T> the trackable of the resource
 */
@Produces(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_JSON })
@Consumes(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_JSON })
public abstract class TrackableResource<T extends Trackable> {
	
	/**
	 * @param name
	 * @return XML/JSON representation of an trackable resource
	 */
	@GET
	@Path("/{name}")
	public T getTrackableByName(@PathParam("name") String name) {
		return this.getGetCommand(name).execute();
	}
	
	protected abstract GetTrackableCommand<T> getGetCommand(String name);
	
	/**
	 * @param name the name of the trackable
	 * @param trackable the trackable to update
	 * @return status
	 */
	@PUT
	@Path("/{name}")
	public Response updateTrackable(@PathParam("name") String name, final T trackable) {
		trackable.setName(name); // trackable names can't be updated
		return this.getUpdateCommand(trackable).execute();
	}
	
	protected abstract UpdateTrackableCommand<T> getUpdateCommand(T trackable);
	
	/**
	 * GET method for retrieving the context of a trackable
	 * @param name
	 * @param contextName
	 * @return the context
	 */
	@Path("/{name}/context/{contextName}")
	@GET
	public Context getTrackableContext(@PathParam("name") String name, @PathParam("contextName") String contextName) {		
		return new GetContextCommand(contextName, name).execute();
	}
	
	/**
	 * Updates one specific context
	 * @param name
	 * @param contextName
	 * @param context
	 * @return the status of the update
	 */
	@Path("/{name}/context/{contextName}")
	@PUT
	public Response updateTrackableContext(@PathParam("name") String name, @PathParam("contextName") final String contextName, final Context context) {
		context.setName(contextName); // trackable context name can't be updated
		return new UpdateContextCommand(name, context).execute();
	}
}
